package com.nlscan.qdkit.ui.publico.util;

import android.text.InputFilter;
import android.text.Spanned;
import android.widget.EditText;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 输入文本规范检测工具类
 * Created by dyp on 2018/2/11.
 * 1. 过滤文本输入
 */

public class InputCheckUtil {

    public static final int DEFAULT_INPUT_NUM_LIMIT = 32;  // 默认的输入字符个数限制

    /**
     * 用户名只能使用字母,数字,下划线，不允许全部数字 (4到20个字符)
     */
    public static boolean checkRegisterUserName(String userName) {
        Pattern pattern = Pattern.compile("^(?![0-9]*$)[a-zA-Z0-9_]{4,20}$");
        return pattern.matcher(userName).matches();
    }

    /**
     * 昵称只能使用字母,数字,下划线，不允许全部数字 (4到20个字符)
     */
    public static boolean checkRegisterNickName(String nickName) {
        Pattern pattern = Pattern.compile("^(?![0-9]*$)[a-zA-Z0-9_]{4,20}$");
        return pattern.matcher(nickName).matches();
    }

    /**
     * 字母,数字或部分特殊符号,不允许全部字母或全部数字
     * -- 注册、修改密码
     */
    public static boolean checkPassword(String password) {
        Pattern pattern = Pattern.compile("^(?![0-9]*$)(?![a-zA-z]*$).{6,16}$");
        return pattern.matcher(password).matches();
    }


    public static void setEditTextInputFilter(EditText editText) {
        setEditTextInputFilter(editText, DEFAULT_INPUT_NUM_LIMIT);
    }

    /**
     * 过滤EditText输入内容，并对长度做限制
     * 只允许输入
     * 1. 数字
     * 2. 大小写字母英文
     * 3. 特殊字符（不允许输入空格），一共支持 35 个特殊字符
     * 新增
     * 32:  (空格)
     * 165:  ¥
     * 183:  ·
     * 8364: € （欧元）
     *
     * @param editText  输入框
     * @param maxLength 最大输入长度，默认最大长度为32个字符
     */
    public static void setEditTextInputFilter(EditText editText, int maxLength) {
        if (editText == null) return;

        InputFilter inputFilter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source == null) return null;

                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < source.length(); i++) {
                    char asciiChar = source.charAt(i);
                    if ((asciiChar >= 32 && asciiChar <= 59)
                            || asciiChar == 61
                            || (asciiChar >= 63 && asciiChar <= 126)
                            || asciiChar == 162
                            || asciiChar == 163
                            || asciiChar == 165
                            || asciiChar == 8364
                            || asciiChar == 8369) {
                        stringBuilder.append(asciiChar);
                    }
                }
                return stringBuilder;
            }
        };
        InputFilter lengthFilter = new InputFilter.LengthFilter(maxLength);

        editText.setFilters(new InputFilter[]{inputFilter, lengthFilter});
    }

    /**
     * 过滤特殊字符串，克制最大字符串字符长度和最大字节数
     *
     * @param editText
     * @param maxLength
     * @param maxByte
     */
    public static void setEditTextInputFilter(EditText editText, int maxLength, int maxByte) {
        if (editText == null) return;

        InputFilter inputFilter = (source, start, end, dest, dstart, dend) -> {
            if (source == null) return null;

            if (((source.length() + dest.length()) > maxLength) || (source.toString().getBytes().length + dest.toString().getBytes().length) > maxByte) {
                return "";
            }

            if (source.toString().contentEquals("\n")) {
                return "";
            }

            String speChar = "[`~!@#$%^&*()+=|{}':;\\\\\"',\\[\\].<>/?~！@#￥%……&*（）+|{}【】‘；：”“’。，\\\\、？]";
            Pattern pattern = Pattern.compile(speChar);
            Matcher matcher = pattern.matcher(source.toString());
            if (matcher.find()) {
                return "";
            } else {
                return null;
            }
        };

        editText.setFilters(new InputFilter[]{inputFilter});
    }

}
